﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BattleShips.Common;
using TestServer;

namespace BattleShips.GameServers.CommandRunners {
    class SelectField : ICommandRunner {
        public string RunCommand(Command command, GameClient client, GameServer server) {
            int readyplayers = 0; uint gameRoomId = 0;
            Dictionary<uint, GameRoom> rooms = new Dictionary<uint, GameRoom>();
            string[] selectedFields = command.Message.Split(':');
            client.clientGameState.selectedFields.Add(selectedFields[0].Trim());
            client.clientGameState.selectedFields.Add(selectedFields[1].Trim());
            client.clientGameState.selectedFields.Add(selectedFields[2].Trim());
            client.PlayerStatus = "Ready";
            rooms = server.GetRooms();
            foreach (var item in rooms.Values) {
                foreach (var item2 in item.GetRoomClients()) {
                    if (item2.Value.Permission.Trim().Equals("GameRoomAdmin")) {
                        gameRoomId = item2.Value.IdPlayer;
                    }
                    if (item2.Value.PlayerStatus.Trim().Equals("Ready")) {
                        readyplayers++;
                        if (readyplayers == 2) {
                            server.StartGame(gameRoomId);
                        }
                    }
                }
            }
            return command.Message;
        }
    }
}
